package com.txw.travel.controller.backstage;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.txw.travel.pojo.Category;
import com.txw.travel.pojo.Product;
import com.txw.travel.service.CategoryService;
import com.txw.travel.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/backstage/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private CategoryService categoryService;

    @RequestMapping("/all")
    public ModelAndView all(@RequestParam(defaultValue = "1") Integer page,
                            @RequestParam(defaultValue = "5") Integer size) {
        ModelAndView modelAndView = new ModelAndView();
        Page<Product> categoryPage = productService.getProductPage(page, size);
        modelAndView.addObject("productPage", categoryPage);
        modelAndView.setViewName("/backstage/product_all");
        return modelAndView;
    }


    @RequestMapping("/addPage")
    public ModelAndView addPage() {
        ModelAndView modelAndView = new ModelAndView();
        List<Category> categoryList = categoryService.getCategoryList();
        modelAndView.addObject("categoryList", categoryList);
        modelAndView.setViewName("/backstage/product_add");
        return modelAndView;
    }

    @RequestMapping("/add")
    public String add(Product product) {
        productService.insertProduct(product);
        return "redirect:/backstage/product/all";
    }
//
//    @RequestMapping("/edit")
//    public ModelAndView edit(Integer pid) {
//        ModelAndView modelAndView = new ModelAndView();
//        Product product = productService.getProductById(pid);
//        modelAndView.addObject("product",product);
//        modelAndView.setViewName("/backstage/product_edit");
//        return modelAndView;
//    }
//
//    @RequestMapping("/update")
//    public String update(Product product) {
//        productService.updateProduct(product);
//        return "redirect:/backstage/product/all";
//    }
//
//    @RequestMapping("/delete")
//    public String delete(Integer pid) {
//        productService.deleteProduct(pid);
//        return "redirect:/backstage/product/all";
//    }
}
